<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
      <el-form-item label="状态" prop="releaseStatus">
        <el-select v-model="queryParams.releaseStatus" placeholder="请选择状态" clearable class="!w-240px">
          <el-option v-for="dict in getIntDictOptions(DICT_TYPE.PUBLISH_STATUS)" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="申请时间" prop="applyTime">
        <el-date-picker v-model="queryParams.applyTime" value-format="YYYY-MM-DD HH:mm:ss" type="daterange"
          start-placeholder="开始日期" end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]" class="!w-240px" />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" /> 搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" /> 重置
        </el-button>
        <el-button type="primary" @click="push('/tag/tagManage')">
          标签管理
        </el-button>
        <el-button @click="push('/apihub')">
          接口开放中心
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" ref="tableRef" row-key="id">
      <el-table-column label="标题" align="center" prop="title" show-overflow-tooltip />
      <el-table-column label="状态" align="center" prop="releaseStatus">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.PUBLISH_STATUS" :value="scope.row.releaseStatus" />
        </template>
      </el-table-column>
      <el-table-column label="标签" align="center" prop="tags">
        <template #default="scope">
          <el-tag type="primary" v-for="tag in scope.row.tags" :key="tag"> {{ tag }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="申请人" align="center" prop="updaterName" />
      <el-table-column label="申请时间" align="center" prop="updateTime" :formatter="dateFormatter" />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" @click="addTag(scope.row.id)">
            添加标签
          </el-button>
          <el-button v-if="scope.row.releaseStatus === 1" link type="success" @click="handelPass(scope.row.id)">
            通过
          </el-button>
          <el-button v-if="scope.row.releaseStatus === 1" link type="danger" @click="handelRefuse(scope.row.id)">
            拒绝
          </el-button>
          <el-button v-if="scope.row.releaseStatus === 2" link type="primary" @click="handelCancel(scope.row.id)">
            取消发布
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
  </ContentWrap>

  <!-- 拒绝审核/取消发布 -->
  <CloseReason ref="closeReasonRef" @success="getList" />
  <!-- 添加标签 -->
  <addTagForm ref="formRef" @success="getList" />
</template>

<script lang="ts" setup>
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import * as PublishApi from '@/api/publish/index'
import CloseReason from './CloseReason.vue'
import addTagForm from './addTagForm.vue'
import { ElMessageBox } from 'element-plus'

defineOptions({ name: 'Message' })
const { push } = useRouter()
const message = useMessage() // 消息
const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  releaseStatus: undefined,
  applyTime: []
})
const queryFormRef = ref() // 搜索的表单
const tableRef = ref() // 表格的 Ref
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  tableRef.value.clearSelection()
  handleQuery()
}
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await PublishApi.getList(queryParams)
    const tagList = await PublishApi.getTagList({
      pageNo: 1,
      pageSize: 10,
    })
    data.records.forEach((item) => {
      if (!item.tagIds) return
      item.tags = []
      JSON.parse(item.tagIds).filter((tab) => {
        tagList.records.filter((tag) => {
          if (tab === tag.id) {
            item.tags.push(tag.tagName)
          }
        })
      })
    })
    list.value = data.records
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 审核操作 */
const handelPass = async (id) => {
  ElMessageBox.confirm('是否确认通过审核？', '审核通过', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(async () => {
      await PublishApi.verifyPublish(
        {
          id: id,
          status: 2
        }
      )
      message.success('审核成功')
      getList()
    })
    .catch((error) => {
      console.log(error)
    })
}

/** 拒绝操作 */
const closeReasonRef = ref()
const handelRefuse = async (id) => {
  closeReasonRef.value.open(id, 'refuse')
}

/** 取消操作 */
const handelCancel = async (id) => {
  closeReasonRef.value.open(id, 'cancel')
}

/** 添加标签 */
const formRef = ref()
const addTag = async (id) => {
  formRef.value.open(id)
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
